//生成一个材料
function createMasonry1Html(i) {
	var html = '';
	html += '<div class="_masonry-box">';
	html += '<a href="/FromPCWeb/res-detail.html"><img class="_box-img" src="/FromPCWeb/img/resource/' + (Math.floor(Math.random() * 10 + 1)) + '.jpg"/></a>';
	html += '<div class="_box-intro">';
	html += '<p class="_box-name">琉璃料管废料</p>';
	html += '<div class="_box-place">';
	html += '<div class="_box-place-l">淄博</div>';
	html += '<div class="_box-place-r"><span class="glyphicon glyphicon-heart"></span>235</div>';
	html += '</div>';
	html += '<p class="_box-class"><琉璃></p>';
	html += '</div>';
	html += '</div>';
	return html;
}

function createMasonry1HtmlCheck(i) {
	var html = '';
	html += '<div class="_masonry-box">';
	html += '<a href="/FromPCWeb/res-detail.html"><img class="_box-img" src="/FromPCWeb/img/resource/' + (Math.floor(Math.random() * 10 + 1)) + '.jpg"/></a>';
	html += '<div class="_box-intro">';
	html += '<p class="_box-name">琉璃料管废料</p>';
	html += '<div class="_box-place">';
	html += '<div class="_box-place-l">淄博</div>';
	html += '<div class="_box-place-r"><span class="glyphicon glyphicon-heart"></span>235</div>';
	html += '</div>';
	html += '<p class="_box-class"><琉璃></p>';
	html += '</div>';
	html += '<div class="_box-check"><img src="/FromPCWeb/img/icon/picked.png" width="25px" height="25px"/></div>';
	html += '</div>';
	return html;
}

//生成一个工厂
function createMasonry2Html(i, isMasonry) {
	var html = '';
	if(isMasonry == false) {
		html += '<div class="_list-item">';
	}
	html += '<div class="_masonry-box">';
	html += '<a href="/FromPCWeb/factory-detail.html"><img class="_box-img" src="/FromPCWeb/img/resource/' + (Math.floor(Math.random() * 10 + 1)) + '.jpg" /></a>';
	html += '<div class="_box-twoimg">';
	html += '<div class="_box-twoimg-l"><img src="/FromPCWeb/img/resource/' + (Math.floor(Math.random() * 10 + 1)) + '.jpg" onclick="changeFactoryImg(this)"></div>';
	html += '<div class="_box-twoimg-r"><img src="/FromPCWeb/img/resource/' + (Math.floor(Math.random() * 10 + 1)) + '.jpg" onclick="changeFactoryImg(this)"></div>';
	html += '</div>';
	html += '<div class="_box-intro">';
	html += '<p class="_box-name">黑蝶珍珠泥</p>';
	html += '<div class="_box-place">';
	html += '<div class="_box-place-l">杭州</div>';
	html += '<div class="_box-place-r"><span class="glyphicon glyphicon-heart"></span>235</div>';
	html += '</div>';
	html += '<p class="_box-class"><建筑材料></p>';
	html += '</div>';
	html += '</div>';
	if(isMasonry == false) {
		html += '</div>';
	}
	return html;
}

function createMasonry2HtmlCheck(i, isMasonry) {
	var html = '';
	if(isMasonry == false) {
		html += '<div class="_list-item">';
	}
	html += '<div class="_masonry-box" title="' + i + '" onclick="check(this)">';
	html += '<a href="/FromPCWeb/factory-detail.html"><img class="_box-img" src="/FromPCWeb/img/resource/' + (Math.floor(Math.random() * 10 + 1)) + '.jpg" /></a>';
	html += '<div class="_box-twoimg">';
	html += '<div class="_box-twoimg-l"><img src="/FromPCWeb/img/resource/' + (Math.floor(Math.random() * 10 + 1)) + '.jpg" onclick="changeFactoryImg(this)"></div>';
	html += '<div class="_box-twoimg-r"><img src="/FromPCWeb/img/resource/' + (Math.floor(Math.random() * 10 + 1)) + '.jpg" onclick="changeFactoryImg(this)"></div>';
	html += '</div>';
	html += '<div class="_box-intro">';
	html += '<p class="_box-name">黑蝶珍珠泥</p>';
	html += '<div class="_box-place">';
	html += '<div class="_box-place-l">杭州</div>';
	html += '<div class="_box-place-r"><span class="glyphicon glyphicon-heart"></span>235</div>';
	html += '</div>';
	html += '<p class="_box-class"><建筑材料></p>';
	html += '</div>';
	html += '<div class="_box-check"><img src="/FromPCWeb/img/icon/pick.png" width="25px" height="25px"/></div>';
	html += '</div>';
	if(isMasonry == false) {
		html += '</div>';
	}
	return html;
}

function createMasonry1HtmlCheck(i, isMasonry) {
	var html = '';
	if(isMasonry == false) {
		html += '<div class="_list-item">';
	}
	html += '<div class="_masonry-box" title="' + i + '" onclick="check(this)">';
	html += '<a href="/FromPCWeb/res-detail.html"><img class="_box-img" src="/FromPCWeb/img/resource/' + (Math.floor(Math.random() * 10 + 1)) + '.jpg" /></a>';
	html += '<div class="_box-twoimg">';
	html += '<div class="_box-twoimg-l"><img src="/FromPCWeb/img/resource/' + (Math.floor(Math.random() * 10 + 1)) + '.jpg" onclick="changeFactoryImg(this)"></div>';
	html += '<div class="_box-twoimg-r"><img src="/FromPCWeb/img/resource/' + (Math.floor(Math.random() * 10 + 1)) + '.jpg" onclick="changeFactoryImg(this)"></div>';
	html += '</div>';
	html += '<div class="_box-intro">';
	html += '<p class="_box-name">黑蝶珍珠泥</p>';
	html += '<div class="_box-place">';
	html += '<div class="_box-place-l">杭州</div>';
	html += '<div class="_box-place-r"><span class="glyphicon glyphicon-heart"></span>235</div>';
	html += '</div>';
	html += '<p class="_box-class"><建筑材料></p>';
	html += '</div>';
	html += '<div class="_box-check"><img src="/FromPCWeb/img/icon/pick.png" width="25px" height="25px"/></div>';
	html += '</div>';
	if(isMasonry == false) {
		html += '</div>';
	}
	return html;
}

function changeFactoryImg(thisObj) {
	console.log($(thisObj).attr('src'));
	$(thisObj).parent().parent().parent().find('._box-img').height($(thisObj).parent().parent().parent().find('._box-img').height()).attr('src', $(thisObj).attr('src'));
}

function check(thisObj) {
	var id = $(thisObj).attr('title').split('|')[0];
	$(thisObj).toggleClass('checked');
	if($(thisObj).hasClass('checked')) {
		$(thisObj).find('._box-check>img').attr('src', '/FromPCWeb/img/icon/picked.png');
	} else {
		$(thisObj).find('._box-check>img').attr('src', '/FromPCWeb/img/icon/pick.png');
	}
}

//首页
//生成一个材料项
function createResHtml() {
	var html = '';
	html += '<div class="_masonry-box">';
	html += '<a href="/FromPCWeb/res-detail.html?id=1"><img class="_box-img" src="/FromPCWeb/img/resource/' + (Math.floor(Math.random() * 10 + 1)) + '.jpg"/></a>';
	html += '<div class="_box-intro">';
	html += '<p class="_box-name"><a href="/FromPCWeb/factory-detail.html?id=2">琉璃料管废料</a></p>';
	html += '<div class="_box-place">';
	html += '<div class="_box-place-l"><a href="/FromPCWeb/index.html?type=1&city=12">杭州</a></div>';
	html += '<div class="_box-place-r"><span class="glyphicon glyphicon-heart"></span>235</div>';
	html += '</div>';
	html += '<p class="_box-class"><a href="/FromPCWeb/index.html?type=1&cate=1"><琉璃></a></p>';
	html += '</div>';
	html += '</div>';
	return html;
}


//首页
//生成一个工厂项
function createFactoryHtml() {
	var html = '';
	html += '<div class="_masonry-box">';
	html += '<a href="/FromPCWeb/factory-detail.html?id=23"><img class="_box-img" height="96px" src="/FromPCWeb/img/resource/' + (Math.floor(Math.random() * 10 + 1)) + '.jpg" /></a>';
	html += '<div class="_box-twoimg">';
	html += '<div class="_box-twoimg-l"><img src="/FromPCWeb/img/resource/' + (Math.floor(Math.random() * 10 + 1)) + '.jpg" onclick="changeFactoryImg(this)"></div>';
	html += '<div class="_box-twoimg-r"><img src="/FromPCWeb/img/resource/' + (Math.floor(Math.random() * 10 + 1)) + '.jpg" onclick="changeFactoryImg(this)"></div>';
	html += '</div>';
	html += '<div class="_box-intro">';
	html += '<p class="_box-name">朱炳仁金星铜工厂</p>';
	html += '<div class="_box-place">';
	html += '<div class="_box-place-l">杭州</div>';
	html += '<div class="_box-place-r"><span class="glyphicon glyphicon-heart"></span>235</div>';
	html += '</div>';
	html += '<p class="_box-class"><金属工艺></p>';
	html += '</div>';
	html += '</div>';
	return html;
}

//瀑布流效果
//masonryBox-装瀑布流的盒子，type-类型：1-材料，2-制造厂
function masonry(masonryBox, type) {
	var html = '';
	for(var i = 1; i <= 20; i++) {
		if(type == 1) {
			html += createResHtml();
		} else if(type == 2) {
			html += createFactoryHtml();
		}
	}
	masonryBox.html(html).show(function() {
		var gutter = 14;
		var centerFunc, $top0;
		masonryBox.masonry({
			columnWidth: '._masonry-box',
			itemSelector: "._masonry-box",
			gutter: gutter,
			isAnimated: true
		});

		masonryBox.imagesLoaded(function()  {        
			masonryBox.masonry({                
				columnWidth: '._masonry-box',
				itemSelector: "._masonry-box",
				gutter: gutter,
				isAnimated: true            
			});     
		});

		//瀑布流居中显示
		centerFunc = function() {
			$top0 = masonryBox.children("[style*='top: 0']");
			masonryBox.css("left", (masonryBox.width() - ($top0.width() * $top0.length + gutter * ($top0.length - 1))) / 2).parent().css("overflow", "hidden");
		};
		centerFunc();
		var tur = true;
		$(window).resize(function() {
			if(tur) {
				setTimeout(function() {
						tur = true;
						centerFunc();
					},
					1000);
				tur = false;
			}
		});
	});

}

//滚动条滚动到底部加载下一页
function loadMoreMasonry(masonryBox, type) {
	$(window).scroll(function() {　　
		var scrollTop = $(this).scrollTop();　　
		var scrollHeight = $(document).height();　　
		var windowHeight = $(this).height();　　
		if(scrollTop + windowHeight == scrollHeight) {　　　　
			console.log("已经到最底部了！");
			var html = '';
			for(var i = 1; i <= 20; i++) {
				if(type == 1) {
					html += createResHtml();
				} else if(type == 2) {
					html += createFactoryHtml();
				}
			}
			var $content = $(html);
			masonryBox.append($content).masonry('appended', $content);
		}
	});
}

//获取URL参数
function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if(r != null) return unescape(r[2]);
	return null;
}